﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SDATA;
using SDLL;

namespace SABI
{
    public partial class frm0001A : Form
    {
        public frm0001A()
        {
            InitializeComponent();
        }

        string sql = "";

        private void frm0001A_Load(object sender, EventArgs e)
        {

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            (Application.OpenForms["frmHome"] as frmHOME).CloseTab(this);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckInput()) { return; }
            FRM.DivisionID = TDBCDivisionID.Text;
            FRM.Month = TDBCMonthYear.Text.Substring(0, 2);
            FRM.Year = TDBCMonthYear.Text.Substring(3, 4);
            FRM.CurrencyID = TDBCboCurrencyID.Text;
            FRM.PaymentTermID = TDBCboPaymentTermID.Text;
            FRM.Report15or48 = txtReport15or48.Text;
            //update
            sql = "UPDATE tb0000 ";
            sql += string.Format("SET DefDivisionID='{0}', DefTranMonth='{1}', DefTranYear='{2}', CurrencyID ='{3}',", FRM.DivisionID, FRM.Month, FRM.Year, FRM.CurrencyID);
            sql += string.Format("PaymentTermID='{0}', Report15or48=N'{1}'", FRM.PaymentTermID, FRM.Report15or48);
            if (DATA.ExecuteNonQuery(sql, FRM.ConnectString, false))
            {
                DLL02.Info("Cập nhật dữ liệu thành công!");
                (Application.OpenForms["frmHOME"] as frmHOME).loadGUI_Form();
            }
            else
                DLL02.Alert("Cập nhật dữ liệu thất bại!");
        }

        bool CheckInput()
        {
            if(TDBCDivisionID.Text=="")
            {
                DLL02.Alert("Bạn phải chọn Mã đơn vị!");
                TDBCDivisionID.Focus();
                return false;
            }
            else if(TDBCMonthYear.Text=="")
            {
                DLL02.Alert("Bạn phải chọn Kỳ kế toán");
                TDBCMonthYear.Focus();
                return false;
            }
            return true;
        }

        private void frm0001A_Shown(object sender, EventArgs e)
        {
            loadDATA_Combo();
        }

        void loadDATA_Combo()
        {
            // Đơn vị
            sql = "SELECT DivisionID, DivisionName FROM tb0038 WHERE Disabled = 0 ORDER BY DivisionID";
            TDBCDivisionID.DataSource = FRM.dt_GET(ref FRM.dt_Division, sql);
            TDBCDivisionID.SelectedValue = FRM.DivisionID;

            // Kỳ kế toán
            sql = "SELECT DISTINCT";
            sql += " MonthYear , TranMonth , TranYear FROM vi0317 WHERE DivisionID = '" + FRM.DivisionID + "'";
            sql += " ORDER BY TranYear DESC, TranMonth DESC";
            DataTable dt_Period = FRM.GetDataTable(sql);
            TDBCMonthYear.DataSource = dt_Period;
            TDBCMonthYear.SelectedValue = FRM.Month.PadLeft(2, '0') + "/" + FRM.Year;

            // loại tiền
            sql = "Select * From tb0024 Where Disabled =0 Order by CurrencyID";
            TDBCboCurrencyID.DataSource = FRM.dt_GET(ref FRM.dt_Currency, sql);
            TDBCboCurrencyID.SelectedValue = FRM.CurrencyID;

            // điều kiện thanh toán
            sql = "SELECT PaymentTermID,PaymentTermName,Duedays,Duetype FROM tb0051 WHERE Disabled=0 ORDER BY PaymentTermID";
            TDBCboPaymentTermID.DataSource = FRM.dt_GET(ref FRM.dt_PaymentTerm, sql);
            TDBCboPaymentTermID.SelectedValue = FRM.PaymentTermID;

            txtReport15or48.Text = FRM.Report15or48;
        }

        private void TDBCDivisionID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtDivisionName.Text = TDBCDivisionID.SelectedItem != null ? TDBCDivisionID.SelectedItem.ItemData[1].ToString() : "";
        }

        private void TDBCboPaymentTermID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtPaymentTermID.Text = TDBCboPaymentTermID.SelectedItem != null ? TDBCboPaymentTermID.SelectedItem.ItemData[1].ToString() : "";
        }

    }
}
